﻿using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System.Text;

namespace LiuQiuRong.Chat.Utilities.Helper
{
    public static class DistributedCacheExtensionsHelper
    {
        public static void SetValue<T>(this IDistributedCache cache, string key, T value)
        {
            cache.Set(key, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)));
        }

        public static T GetValue<T>(this IDistributedCache cache, string key)
        {
            var value = cache.Get(key);

            return value == null ? default : JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(value));
        }
    }
}
